/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;
import DTO.Group_user;
import java.sql.ResultSet;
import java.util.List;
import DTO.CFields;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Group_userBUS extends IData<Group_user>{

    @Override
    public Group_user add(Group_user item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected void add(Group_user item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<Group_user> list, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Group_user Find(Group_user item, List<Group_user> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Object OnSubmit(Group_user item, int t, SubConfig config) {
        Object result = 0;
        String mysql ="INSERT INTO group_user(" + CFields.group_name + "," + CFields.note +")" + " VALUES (?,?);";                      
        try {   
            PreparedStatement cs;
            if(t == 0){                
                cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
                cs.setString(1, item.getGroup_name());
                cs.setString(2, item.getNote());
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1);      
                }
            }
            else if(t== 1){
                mysql = "update group_user set " + CFields.group_name + "=?,"
                        + CFields.note + "=? where " + CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);
                cs.setString(1, item.getGroup_name());
                cs.setString(2, item.getNote());
                cs.setLong(3, item.getId());
                result = cs.executeUpdate();
            }
            else{
                mysql = "delete from group_user where "+ CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);
                cs.setLong(1, item.getId());
                result = cs.executeUpdate();
            }
        } catch (SQLException ex) {            
            Logger.getLogger(Group_userBUS.class.getName()).log(Level.SEVERE, null, ex);            
        }        
        return result;
    }

    @Override
    public Object insertOnSubmit(Group_user item, SubConfig config) {
        return this.OnSubmit(item, 0, config);
    }

    @Override
    public Object updateOnSubmint(Group_user item, SubConfig config) {
        return this.OnSubmit(item, 1, config);
    }

    @Override
    public Object deleteOnSubmint(Group_user item, SubConfig config) {
        return this.OnSubmit(item, 2, config);
    }

    @Override
    protected Group_user getItem(ResultSet rs) {
        Group_user item = new Group_user();
        try {
            item.setGroup_name(rs.getString(CFields.group_name));
            item.setId(rs.getLong(CFields.id));
            item.setNote(CFields.note);
        } catch (SQLException ex) {
            Logger.getLogger(Group_userBUS.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public List<Group_user> getAll(SubConfig config) {
        String mysql = "select * from " + CFields.tb_group_user;
        return this.getAll(mysql, config);
    }       
}
